package com.cutefool.zero.code.admin.company.web;

import com.cutefool.core.account.Account;
import com.cutefool.core.account.LoginAccountService;
import com.cutefool.core.res.PageResponse;
import com.cutefool.core.res.Response;
import com.cutefool.dsl.mybatis.core.dsl.builder.DeleteDsl;
import com.cutefool.dsl.mybatis.core.dsl.builder.InsertDsl;
import com.cutefool.dsl.mybatis.core.dsl.builder.QueryDsl;
import com.cutefool.dsl.mybatis.core.dsl.builder.UpdateDsl;
import com.cutefool.dsl.mybatis.core.dsl.condition.Conditioning;
import com.cutefool.dsl.mybatis.core.dsl.operation.CuteCurdOperation;
import com.cutefool.dsl.mybatis.core.page.Paging;
import com.cutefool.zero.code.admin.company.domain.Company;
import com.cutefool.zero.code.admin.company.dto.CompanyDTO;
import com.cutefool.zero.code.admin.consts.Constants;
import jakarta.annotation.Resource;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

;

/**
 * 相关接口
 *
 * @author : 271007729@qq.com
 */
@RestController
@RequestMapping(Constants.API_PREFIX)
public class CompanyController {

    @Resource
    private LoginAccountService<Account> loginAccountService;

    @Resource
    private CuteCurdOperation<Company> cuteCurdOperation;

    /**
     * 新增
     *
     * @param dto dto
     * @return Response<Long>
     **/
    @PostMapping("/add/companies")
    public Response<Long> create(@RequestBody @Validated CompanyDTO dto) {
        InsertDsl<Company> entity = InsertDsl.newInstance(Company.instance()).entity(dto);
        this.cuteCurdOperation.create(entity);
        return Response.ok(entity.getEntity().getId());
    }

    /**
     * 更新
     *
     * @param id  id
     * @param dto dto
     * @return Response<Long>
     **/
    @PostMapping("/update/companies/{id}")
    public Response<Long> update(@PathVariable Long id, @RequestBody @Validated CompanyDTO dto) {
        UpdateDsl<Company> updateDsl = UpdateDsl.newInstance(Company.instance()).entity(dto);
        this.cuteCurdOperation.update(updateDsl);
        return Response.ok(id);
    }

    /**
     * 删除
     *
     * @param id id
     * @return Response<Boolean>
     **/
    @PostMapping("/delete/companies/{id}")
    public Response<Boolean> delete(@PathVariable Long id) {
        DeleteDsl<Company> deleteDsl = DeleteDsl.newInstance(Company.instance(), (where) -> where.and(Company.instance().id(), SqlBuilder.isEqualTo(id)));
        this.cuteCurdOperation.delete(deleteDsl);
        return Response.ok();
    }


    /**
     * 查询（通过ID）
     *
     * @param id id
     * @return Response<Company>
     **/
    @GetMapping("/info/companies/{id}")
    public Response<Company> byId(@PathVariable Long id) {
        QueryDsl<Company> queryDsl = QueryDsl.newInstance(Company.instance(), where -> where.and(Company.instance().id(), SqlBuilder.isEqualTo(id)));
        Optional<Company> optional = this.cuteCurdOperation.get(queryDsl);
        return Response.ok(optional.orElse(null));
    }

    /**
     * 查询（列表）
     *
     * @param query query
     * @return Response<List < Company>>
     **/
    @GetMapping("/list/companies")
    public Response<List<Company>> list(Conditioning query) {
        query.setCreateBy(loginAccountService.getPresent().getId());
        QueryDsl<Company> queryDsl = QueryDsl.newInstance(Company.instance(), query);
        List<Company> entities = this.cuteCurdOperation.list(queryDsl);
        return Response.ok(entities);
    }

    /**
     * 查询（分页）
     *
     * @param query  query
     * @param paging paging
     * @return Response<PageResponse < Company>>
     **/
    @GetMapping("/page/companies")
    public Response<PageResponse<Company>> page(Conditioning query, Paging paging) {
        query.setCreateBy(loginAccountService.getPresent().getId());
        QueryDsl<Company> queryDsl = QueryDsl.newInstance(Company.instance(), query, paging);
        PageResponse<Company> response = this.cuteCurdOperation.page(queryDsl);
        return Response.ok(response);
    }
}
